package cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.back.util;

import com.paypal.core.PayPalEnvironment;
import com.paypal.core.PayPalHttpClient;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

@Slf4j
public class PayPalClient {

    public PayPalHttpClient client(String mode, String clientId, String clientSecret) {
        log.info("mode={}, clientId={}, clientSecret={}", mode, clientId, clientSecret);
        PayPalEnvironment environment = mode.equals("live") ? new PayPalEnvironment.Live(clientId, clientSecret) : new PayPalEnvironment.Sandbox(clientId, clientSecret);
        return new PayPalHttpClient(environment);
    }

//    /**
//     * @param jo
//     * @param pre
//     * @return
//     */
//    public String prettyPrint(JSONObject jo, String pre) throws JSONException {
//        Iterator<?> keys = jo.keys();
//        StringBuilder pretty = new StringBuilder();
//        while (keys.hasNext()) {
//            String key = (String) keys.next();
//            pretty.append(String.format("%s%s: ", pre, StringUtils.capitalize(key)));
//            if (jo.get(key) instanceof JSONObject) {
//                pretty.append(prettyPrint(jo.getJSONObject(key), pre + "\t"));
//            } else if (jo.get(key) instanceof JSONArray) {
//                int sno = 1;
//                JSONArray jsonArray = jo.getJSONArray(key);
//                for (Object jsonObject : jsonArray) {
//                    pretty.append(String.format("\n%s\t%d:\n", pre, sno++));
//                    pretty.append(prettyPrint((JSONObject) jsonObject, pre + "\t\t"));
//                }
//            } else {
//                pretty.append(String.format("%s\n", jo.getString(key)));
//            }
//        }
//        return pretty.toString();
//    }
}